package com.inspur.gs.sbssdl.dzyhpgl.dzyhphz.dzyhphz.back.bizactions;
import com.inspur.edp.bef.api.lcp.IStandardLcp;
import com.inspur.edp.bef.core.LcpUtil;
import com.inspur.edp.cef.api.message.*;
import com.inspur.edp.bef.api.action.determination.*;
import com.inspur.edp.bef.api.action.validation.*;
import com.inspur.edp.cef.entity.changeset.*;
import com.inspur.edp.bef.api.be.*;
import com.inspur.edp.bef.api.action.VoidActionResult;
import com.inspur.edp.bef.spi.action.AbstractManagerAction;
import com.inspur.edp.cef.entity.entity.IEntityData;
import com.inspur.edp.cef.entity.entity.IEntityDataCollection;
import com.inspur.edp.cef.entity.entity.IValuesContainer;

import java.util.ArrayList;

public class sbssYhpHzdhfKcslMgrAction extends AbstractManagerAction<VoidActionResult> {
	private String id;
	public sbssYhpHzdhfKcslMgrAction(IBEManagerContext managerContext, String id) {
		super(managerContext);
		this.id = id;
	}
	private String hzdCfg = "com.inspur.gs.sbssdl.dzyhpgl.dzyhphz.dzyhphz.back.sbssYhpHzd";
	private String kcCfg = "com.inspur.gs.sbssdl.dzyhpgl.dzyhyu.dzyhyu.back.sbssYhpItem";


	/**
	 * 恢复库存
	 */
	@Override
	public  void execute() {
		IStandardLcp lcp = LcpUtil.getLcp(hzdCfg);
		IStandardLcp kcLcp = LcpUtil.getLcp(kcCfg);
		// hzd
		IEntityData hzdData = lcp.retrieve(this.id).getData();
		IEntityDataCollection hzdMx = hzdData.getChilds().get("hzdMx");

		for (IEntityData mx : hzdMx) {
			unlockKc(hzdData,mx,kcLcp);
		}
	}
	private IBusinessEntity getEntity(String dataId) {
		return (IBusinessEntity)super.getBEManagerContext().getEntity(dataId);
	}	private IBEService getMgr() {
		return (IBEService)super.getBEManagerContext().getBEManager();
	}


	private void unlockKc(IEntityData hzdData,IEntityData mx,IStandardLcp kcLcp) {
		String mxId = mx.getID();
		int lockNum = Integer.parseInt(mx.getValue("zcsl").toString());
		IValuesContainer kcdh = (IValuesContainer) mx.getValue("kcdh");
		if (kcdh == null || kcdh.getValue("kcdh") == null) {
			return;
		}
		String kcId = kcdh.getValue("kcdh").toString();
		IEntityData kcData = kcLcp.retrieve(kcId).getData();
		int sysl = Integer.parseInt(kcData.getValue("sysl").toString());

		// 检查扣减日志是否存在,不存在则返回
		IEntityDataCollection itemtras = kcData.getChilds().get("itemtras");
		boolean hasItem = itemtras.stream().anyMatch(item -> item.getValue("djMxid") != null && item.getValue("djMxid").toString().equals(mxId));
		if (!hasItem) return;

		// 归还剩余数量
		ModifyChangeDetail kcMcd = new ModifyChangeDetail(kcId);
		kcMcd.putItem("sysl", sysl + lockNum);

		// 删除日志
		ArrayList<String> nodeCodes = new ArrayList<>();
		nodeCodes.add("itemtras");
		ArrayList<String> hierarchyIds = new ArrayList<>();
		hierarchyIds.add(kcId);

		ArrayList<String> delIds = new ArrayList<>();
		for (IEntityData itemtra : itemtras) {
			if (itemtra.getValue("djMxid") != null){
				String djMxid = itemtra.getValue("djMxid").toString();
				if (djMxid.equals(mxId)) {
					delIds.add(itemtra.getID());
				}
			}
		}

		// 提交
		kcLcp.deleteChild(nodeCodes,hierarchyIds,delIds);
		kcLcp.modify(kcMcd);
		kcLcp.save();
	}
}